package com.yanqu.road.dataHelper.action;

import com.yanqu.road.dataHelper.action.excel.Retained;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.manager.ServerMgr;
import com.yanqu.road.entity.servercenter.ServerInfo;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

public class RetainedExcel extends actionBase{

    public void go(List<Long> serverList){
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        String date = "2021-01-20";
        List<Future> futureList = new ArrayList<>();
        for(Long serverId : serverList){
            if(ServerMgr.getServerInfoList().containsKey(serverId)){
                ServerInfo serverInfo = ServerMgr.getServerInfoList().get(serverId);
                Future future = executorService.submit(new Callable<Boolean>() {
                    @Override
                    public Boolean call() throws Exception {
                        GameServer testGameServer = new GameServer(serverInfo);
                        Retained retained = new Retained(testGameServer);
                        retained.go(date);
                        return true;
                    }
                });
                futureList.add(future);
            }
        }
        isResult(futureList);

        //合并所有区服
        init();
        Map<String,Map<String,Map<String,String>>> dealMap = new ConcurrentHashMap<>();
        List<Map<String,String>> allData = DataMgr.getData();
        for(Map<String,String> tempMap : allData){
            tempMap.remove("区服id");
            if(!dealMap.containsKey(tempMap.get("日期"))){
                dealMap.put(tempMap.get("日期"),new ConcurrentHashMap<>());
            }
            if(!dealMap.get(tempMap.get("日期")).containsKey(tempMap.get("是否加入商会"))){
                dealMap.get(tempMap.get("日期")).put(tempMap.get("是否加入商会"),tempMap);
                continue;
            }
            Map<String,String> dealData = dealMap.get(tempMap.get("日期")).get(tempMap.get("是否加入商会"));
            BigInteger allUserCount = new BigInteger(dealData.get("总用户数"));
            allUserCount = allUserCount.add(new BigInteger(tempMap.get("总用户数")));
            BigInteger userCount = new BigInteger(dealData.get("用户数"));
            userCount = userCount.add(new BigInteger(tempMap.get("用户数")));
            BigInteger payUserCount = new BigInteger(dealData.get("付费用户数"));
            payUserCount = payUserCount.add(new BigInteger(tempMap.get("付费用户数")));
            BigInteger payCount = new BigInteger(dealData.get("付费总金额"));
            payCount = payCount.add(new BigInteger(tempMap.get("付费总金额")));
            dealData.put("总用户数",allUserCount.toString());
            dealData.put("用户数",userCount.toString());
            dealData.put("付费用户数",payUserCount.toString());
            dealData.put("付费总金额",payCount.toString());
            for(int day : retainedList){
                BigInteger tempNum = new BigInteger(tempMap.get(day + "日留存"));
                dealData.put(day + "日留存",tempNum.add(new BigInteger(dealData.get(day + "日留存"))).toString());
            }
        }
        for(String dataDate : dealMap.keySet()){
            Map<String,Map<String, String>> tempData = dealMap.get(dataDate);
            for(Map<String,String> tempMap : tempData.values()){
                BigDecimal payCount = new BigDecimal(tempMap.get("付费总金额"));
                BigDecimal allUserCount = new BigDecimal(tempMap.get("总用户数"));

                if(allUserCount.compareTo(BigDecimal.ZERO) != 0){
                    tempMap.put("付费ARPPU",payCount.divide(allUserCount,4, BigDecimal.ROUND_UP).toString());
                    for(Integer day : retainedList){
                        BigDecimal tempUser = new BigDecimal(tempMap.get(day+"日留存"));
                        tempMap.put(day+"日留存",tempUser.divide(allUserCount,4, BigDecimal.ROUND_UP).toString());
                    }
                }else{
                    tempMap.put("付费ARPPU","0");
                }
            }
        }
        List<Map<String,String>> excelData = new ArrayList<>();
        for (Map<String,Map<String,String>> a : dealMap.values()){
            excelData.addAll(a.values());
        }
        DataMgr.clearExcelData();
        DataMgr.addData(excelData);
        DataMgr.outExcel(date + "至今的商会数据");
        DataMgr.makeExcelData2Txt("");
    }


    private List<Integer> retainedList = new ArrayList<>();

    private void init(){
        retainedList.add(1);
        retainedList.add(3);
        retainedList.add(7);
        retainedList.add(15);
        retainedList.add(30);
        retainedList.add(60);
        retainedList.add(90);
    }
}
